const models = require('../db/mysqldb/index')
const { Op } = require("sequelize");

class DataPageModel {
  /**
   * @description 通过page_id查找数据，不包含删除数据
   * @param {string} page_id 
   * @returns 
   */
  static async findByPageId (page_id,) {
    return await models.page.findOne({
      where: {
        delete_timestamp: {
          [Op.is]: null,
        },
        page_id
      }
    })
  }
  /**
   * @description 通过 project_id 查找数据，不包含删除数据
   * @param {string} project_id 
   * @returns 
   */
  static async findByProjectId (project_id,) {
    return await models.page.findAll({
      where: {
        delete_timestamp: {
          [Op.is]: null,
        },
        project_id,
      }
    })
  }
  /**
   * @description 通过name和id查找数据，不包含删除数据
   * @param {string} name 
   * @param {string} project_id 
   * @returns 
   */
  static async findByNameAndProjectId (name, project_id,) {
    return await models.page.findOne({
      where: {
        project_id,
        name,
        delete_timestamp: {
          [Op.is]: null,
        }
      }
    })
  }
  /**
  * @description 通过name和id查找数据，不包含删除数据
  * @param {string} name 
  * @param {string} project_id 
  * @returns 
  */
  static async findByNameAndProjectIdNePageId (name, project_id, page_id) {
    return await models.page.findOne({
      where: {
        project_id,
        name,
        delete_timestamp: {
          [Op.is]: null,
        },
        page_id: {
          [Op.ne]: page_id,
        }
      }
    })
  }
  /**
   * @description create
   * @param {*} name 
   * @param {*} project_id 
   * @param {*} thumb 
   * @param {*} remark 
   * @param {*} op_user_id 
   * @returns 
   */
  static async create (name, project_id, thumb, remark, op_user_id, layout) {
    return await models.page.create({
      project_id,
      name,
      thumb,
      remark,
      layout,
      create_by: op_user_id,
    })
  }
  /**
   * @description blukCreate
   * @param {*} pages 
   * @param {*} op_user_id 
   * @returns 
   */
  static async bulkCreate (pages) {
    return await models.page.bulkCreate(pages)
  }
  /**
   * @description copyByProjectId
   * @param {*} project_id 
   * @param {*} newProjectId 
   * @returns 
   */
  static async copyByProjectId (project_id, newProjectId) {
    let sql = `INSERT INTO page ( NAME, thumb, project_id, publish, remark, page_url,layout, create_timestamp, create_by,delete_timestamp,delete_by ) SELECT NAME,thumb,${newProjectId},publish,remark,page_url,unix_timestamp(),create_by,delete_timestamp,delete_by FROM page WHERE project_id= ${project_id}`
    const result = await models.sequelize.query(sql, { replacements: ['active'], type: models.sequelize.QueryTypes.INSERT })
    return result
  }
  /**
   * @description blukUpdate
   * @param {*} pages 
   * @param {*} op_user_id 
   * @returns 
   */
  static async blukUpdate (pages) {
    return await models.page.bulkCreate(pages, { updateOnDuplicate: ["page_url"] });
  }
  /**
   * @description update
   * @param {*} name 
   * @param {*} thumb 
   * @param {*} remark 
   * @param {*} publish 
   * @param {*} op_user_id 
   * @param {*} page_id 
   * @returns 
   */
  static async update (name, thumb, remark, publish, op_user_id, page_id, layout) {
    return await models.page.update(
      {
        name,
        thumb,
        remark,
        publish,
        layout,
        update_by: op_user_id,
        update_timestamp: Date.now()
      },
      {
        where: { page_id },
      }
    )
  }
  /**
   * @description update page_url
   * @param {*} page_id 
   * @param {*} page_url 
   * @returns 
   */
  static async updatePageURL (page_id, page_url,) {
    return await models.page.update(
      {
        page_url,
      },
      {
        where: { page_id },
      }
    )
  }
  /**
  * @description delete 
  * @param {*} op_user_id 
  * @param {*} page_id 
  * @returns 
  */
  static async delete (op_user_id, page_id,) {
    return await models.page.update(
      {
        delete_timestamp: Date.now(),
        delete_by: op_user_id
      },
      {
        where: { page_id },
      }
    )
  }
  /**
   * @description list 
   * @param {*} limit 
   * @param {*} size 
   * @param {*} name 
   * @param {*} remark 
   * @param {*} project_id 
   * @returns 
   */
  static async list (limit, size = 1, name, remark, project_id,) {
    let sql = `select SQL_CALC_FOUND_ROWS page.page_id,page.name,page.remark,page.thumb,page.layout,page.project_id,page.publish,page.page_url,project.name as project_name from page LEFT JOIN project ON page.project_id = project.project_id where 1 = 1 AND page.delete_timestamp IS NULL AND page.project_id = ${project_id} AND page.page_url IS NOT NULL AND project.delete_timestamp IS NULL `
    if (name) {
      sql = sql + `AND page.name like '%${name}%' `
    }
    if (remark) {
      sql = sql + `AND page.remark like '%${remark}%' `
    }
    if (limit) {
      sql = sql + `LIMIT ${limit} OFFSET  ${(size - 1) * limit} `
    }
    sql = sql + "ORDER BY page.create_timestamp DESC ,page.update_timestamp DESC "
    const result = await models.sequelize.query(sql, { replacements: ['active'], type: models.sequelize.QueryTypes.SELECT })
    const count = await models.sequelize.query(`SELECT FOUND_ROWS() as count;`, { replacements: ['active'], type: models.sequelize.QueryTypes.SELECT })
    return { result, count }
  }
  /**
   * @description listByProjectId 
   * @param {*} limit 
   * @param {*} size 
   * @param {*} name 
   * @param {*} remark 
   * @param {*} project_id 
   * @returns {*} result, count
   */
  static async listByProjectId (size=1,limit,name, remark, project_id) {
    let sql = `select SQL_CALC_FOUND_ROWS page.page_id,page.name,page.remark,page.thumb,page.layout,page.project_id,page.publish,page.page_url,project.name as project_name from page LEFT JOIN project ON page.project_id = project.project_id where 1 = 1 AND page.page_url IS NOT NULL AND page.delete_timestamp IS NULL AND page.project_id = ${project_id}  AND project.delete_timestamp IS NULL `
    if (name) {
      sql = sql + `AND page.name like '%${name}%' `
    }
    if (remark) {
      sql = sql + `AND page.remark like '%${remark}%' `
    }
    sql = sql + "ORDER BY page.create_timestamp DESC ,page.update_timestamp DESC "
    if (limit) {
      sql = sql + `LIMIT ${limit} OFFSET  ${(size - 1) * limit} `
    }
    console.log(sql,"this.listByProjectId",limit);
    const result = await models.sequelize.query(sql, { replacements: ['active'], type: models.sequelize.QueryTypes.SELECT })
    const count = await models.sequelize.query(`SELECT FOUND_ROWS() as count;`, { replacements: ['active'], type: models.sequelize.QueryTypes.SELECT })
    return { result, count }
  }
  /**
 * @description publish 
 * @param {*} op_user_id 
 * @param {*} page_id 
 * @returns 
 */
  static async publish (op_user_id, page_id,) {
    return await models.page.update(
      {
        publish: 1,
        update_by: op_user_id,
        update_timestamp: Date.now()
      },
      {
        where: { page_id },
      }
    )
  }
}
module.exports = {
  DataPageModel,
}